package com.jkhan.demo.threads.pool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName ThreadPool
 * @Description ThreadPool
 * @Author hjk
 * @Date 2022/1/11 21:47
 **/
public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        ThreadPoolExecutor service = (ThreadPoolExecutor) executorService;

        service.setCorePoolSize(5);

        executorService.execute(new PThread());
        executorService.execute(new PThread());
        executorService.submit(new CThread());

        executorService.shutdown();
    }
}

class PThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}

class CThread implements Callable {

    @Override
    public Object call() throws Exception {
        int i;
        for (i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
        return i;
    }
}